package com.eh.observer.jdk_observer.observer;

import com.eh.observer.dto.Event;
import com.eh.observer.ext.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by David on 2016/4/23.
 */
public class JdkCurrentConditionDisplay implements Observer, DisplayElement {
    private Observable observable;
    private float temperature;
    private float humidity;

    @Override
    public void display() {
        System.out.println(String.format("当前天气状况: 温度：【%.2f】, 湿度：【%.2f】.", temperature, humidity));
    }

    @Override
    public void update(Observable o, Object arg) {
        this.observable = o;
        if (arg instanceof Event) {
            Event event = (Event) arg;
            this.temperature = event.getTemperature();
            this.humidity = event.getHumidity();
            display();
        }
    }

    public Observable getObservable() {
        return observable;
    }

    public void setObservable(Observable observable) {
        this.observable = observable;
    }
}
